package sy.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.log4j.Logger;

import sy.dao.SysProjectOwnerMappingDaoI;
import sy.dao.SysUserUnitMappingDaoI;
import sy.model.SysBuilding;
import sy.model.SysProject;
import sy.model.SysProjectInformationMapping;
import sy.model.SysProjectOwnerMapping;
import sy.model.SysUnit;
import sy.model.SysUser;
import sy.model.SysUserUnitMapping;
import sy.service.SysProjectOwnerMappingServiceI;
import sy.service.SysUnitServiceI;
import sy.service.SysUserServiceI;
import sy.service.SysUserUnitMappingServiceI;

@Service("sysUserUnitMappingService")
public class SysUserUnitMappingServiceImpl implements SysUserUnitMappingServiceI {
	
	private static final Logger logger = Logger.getLogger(SysProjectOwnerMapping.class);
	
	@Autowired
	private SysUserUnitMappingDaoI sysUserUnitMappingDao;
	@Autowired
	private SysUnitServiceI sysUnitService;
	@Autowired
	private SysUserServiceI sysUserService;
	@Autowired
	private SysUserUnitMappingServiceI sysUserUnitMappingService;

	@Override
	public Serializable save(SysUserUnitMapping sysUserUnitMapping) {
		
		return sysUserUnitMappingDao.save(sysUserUnitMapping);
	}

	@Override
	public void remove(SysUserUnitMapping sysUserUnitMapping) {
		// TODO Auto-generated method stub
		sysUserUnitMappingDao.remove(sysUserUnitMapping);
	}

	@Override
	public void update(SysUserUnitMapping sysUserUnitMapping) {
		// TODO Auto-generated method stub
		sysUserUnitMappingDao.update(sysUserUnitMapping);
	}

	@Override
	public List<SysUserUnitMapping> list() {
		// TODO Auto-generated method stub
		return sysUserUnitMappingDao.list();
	}

	@Override
	public SysUserUnitMapping getSysUserUnitMappingById(String id) {
		// TODO Auto-generated method stub
		return sysUserUnitMappingDao.getSysUserUnitMappingById(id);
	}

	@Override
	public List<SysUserUnitMapping> listSysUserUnitMappingByKey(String key) {
		// TODO Auto-generated method stub
		return sysUserUnitMappingDao.listSysUserUnitMappingByKey(key);
	}

	@Override
	public List<SysUserUnitMapping> listSysUserUnitMappingByOwnerId(String ownerId) {
		// TODO Auto-generated method stub
		return sysUserUnitMappingDao.listSysUserUnitMappingByOwnerId(ownerId);
	}
	
	@Override
	public Serializable saveSysUserUnitMapping(List<SysUserUnitMapping> sysUserUnitMapping) {
		// TODO Auto-generated method stub
		return sysUserUnitMappingDao.saveSysUserUnitMappings(sysUserUnitMapping);
	}
	
	@Override
	public void removeByUser(String userId) {
		// TODO Auto-generated method stub
		sysUserUnitMappingDao.removeByUser(userId);
	}

	@Override
	public void writeIntoUserUnitMapping(SysUser sysUser,String[] unitIds){	
		/**
		 * 构造用户房址信息中间表（批量）
		 */
		List<SysUserUnitMapping> userUnitList = new ArrayList<SysUserUnitMapping>();
		String uuid = "";
		for(int i = 0; i<unitIds.length; i++){
			//构造中间表
			SysUserUnitMapping userUnit = new SysUserUnitMapping();
			uuid = UUID.randomUUID().toString();
			userUnit.setId(uuid);
			//将User信息写入中间表
			userUnit.setSysUser(sysUser);
			//将单元信息写入中间表
			SysUnit unit = sysUnitService.getSysUnitById(unitIds[i]);
			userUnit.setSysUnit(unit);
			//叠加
			userUnitList.add(userUnit);
		} 
		saveSysUserUnitMapping(userUnitList);
	};
	
	
	public List<String> getProjectsByUserId(String userId){
		
		List<String> addressList = new ArrayList<String>();
		/**
		 * 通过用户id获取账户，再通过账户查询业主单元信息中间表符合条件的项目，得到该用户所属房址信息
		 */
		SysUser user = sysUserService.getSysUserById(userId);
		List<SysUserUnitMapping> userUnitList = sysUserUnitMappingService.listSysUserUnitMappingByOwnerId(user.getId());
		for(SysUserUnitMapping userUnit :userUnitList) {
			
			/**
			 *通过中间表返回单元，通过单元返回楼栋，通过楼栋返回项目
			 */
			String unitId = userUnit.getSysUnit().getId();
			SysUnit unit =sysUnitService.getSysUnitById(unitId);	
			SysBuilding building =unit.getSysBuilding();
			SysProject project = building.getSysProject();
			
			/**
			 * 拼接房址，循环加入房址数组
			 */
			String address = project.getName()+building.getName()+unit.getName();
			addressList.add(address);
		}
		
		return addressList;
	}

}
